package com.fenghongzhang.day006;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main2Activity extends AppCompatActivity implements SurfaceHolder.Callback {
    private Button start;

    private MediaRecorder mediaRecorder;
    private Camera camera;
    private SurfaceView surface;
    private SurfaceHolder surfaceHolder;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        surface = (SurfaceView) findViewById(R.id.surface);
        start = (Button) findViewById(R.id.start);

        surfaceHolder = surface.getHolder();

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mediaRecorder == null){
                    mediaRecorder = new MediaRecorder();
                }

                if(camera == null){
                    camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
                    camera.unlock();
                    camera.setDisplayOrientation(90);
                }
                mediaRecorder.setCamera(camera);

                //设置源
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
                mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

                //输出格式
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

                //编解码类型
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

                //设置属性
                mediaRecorder.setVideoSize(640,480); //每个手机不一样.
                mediaRecorder.setVideoEncodingBitRate(30);
                mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());//

                //视频保存的位置
                //sd 判断sd是否可用
                // \  /
                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                    ///storage/emulated/0/video
                    File file = Environment.getExternalStorageDirectory();
                    //创建文件
                    ///storage/emulated/0/video
                    File file1 = new File(file, File.separator+"video");

                    if(!file1.exists()){
                        file1.mkdirs();
                    }

                    File file2 = new File(file1,File.separator+System.currentTimeMillis()+".mp4");
                    path = file2.getAbsolutePath();

                }
                mediaRecorder.setOutputFile(path);
                try {
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        surfaceHolder = holder;
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        surfaceHolder = holder;
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

    }
}
